package com.c2c.service.impl;

import com.c2c.mapper.GoodsMapper;
import com.c2c.mapper.OrdersMapper;
import com.c2c.mapper.UserMapper;
import com.c2c.po.Goods;
import com.c2c.po.Orders;
import com.c2c.po.User;
import com.c2c.po.enums.impl.GoodiesState;
import com.c2c.po.enums.impl.OrdersState;
import com.c2c.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<Orders> findAllOrder() {
        return ordersMapper.findAllOrder();
    }

    @Override
    public List<Orders> findAllOrderByUserAccountId(int userAccount) {
        return ordersMapper.findAllOrderByUserId(userAccount);
    }

    @Override
    public List<Orders> findAllOrderByGoodsId(int userAccount) {
        return ordersMapper.findAllOrderByGoodsId(userAccount);
    }

    @Override
    @Transactional
    public void updateOrderStateByOrderId(Orders orders, int ordersState) {
        for (OrdersState ordersState1 :OrdersState.values()){
            if (ordersState1.getCode() == ordersState){
                //此时在枚举类中已经找到了对应的值
                //找到对应的订单
                Orders orderByOrderId = ordersMapper.findOrderByOrderId(orders);
                //根据订单找到对应的商品
                Goods byId = goodsMapper.findById(orderByOrderId.getGoodsId());
                if (ordersState1 == OrdersState.RECEIVED){
                    //情况一  双方正常结束订单  需要把在平台扣留的钱给予卖家
                    //订单结束时间
                    orders.setOrderEndTime(new Date());
                    //给Goods中封装的User重新赋值Money  这里的User就是卖家信息
                    byId.getUser().setUserMoney(byId.getGoodsPrice());
                    //更新卖家金钱
                    userMapper.updateMoneyByUserId(byId.getUser(),true);
                }else if(ordersState1 == OrdersState.SUCCESSFULLY_CANCELLED){
                    //情况二  双方正常取消订单  需要把在平台扣留的钱还给买家
                    //订单结束时间
                    orders.setOrderEndTime(new Date());
                    //根据买家ID获取其实体  然后重新给Money赋值
                    User byUserId = userMapper.findByUserId(orderByOrderId.getUserAccount());
                    byUserId.setUserMoney(byId.getGoodsPrice());
                    //更新买家金钱
                    userMapper.updateMoneyByUserId(byUserId,true);
                    //状态改回  把对应商品状态改为通过审核
                    //首先更改获得的实体
                    byId.setGoodsState(GoodiesState.REVIEWED_SUCCESS);
                    //更改状态
                    goodsMapper.updateStateById(byId);
                }
                orders.setOrderState(ordersState1);
                ordersMapper.updateOrderStateByOrderId(orders);
            }
        }
    }

    @Override
    @Transactional
    public void insertOrder(Orders orders) {
        //第一步  找到商品对应价格
        Goods byId = goodsMapper.findById(orders.getGoodsId());
        //第二步  封装其内容成为一个新的User
        User user = new User();
        user.setUserAccount(orders.getUserAccount());//用户ID
        user.setUserMoney(byId.getGoodsPrice());//商品价格
        //第三步  扣除买家的钱
        userMapper.updateMoneyByUserId(user,false);
        //第四步  生成订单
        ordersMapper.insertOrder(orders);
        //第五步  生成订单后  商品状态更改为  已卖出
        byId.setGoodsState(GoodiesState.GOODS_SOLD);
        goodsMapper.updateStateById(byId);
    }
}
